﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Drawing;
using System.Diagnostics;
using System.IO;

namespace Galaxy_Wars
{
    [Serializable]
    public class Planet
    {
        //protected List<Ship> ships = new List<Ship>();
        protected int ships;
        protected int planetnumber;
        protected int playerNumber;
        protected String name;
        protected Rectangle coords;
        protected int dayproduction;
        protected Bitmap planetLooks;

        public Planet(int planetnumber, int playerNumber, string planetName, Rectangle coords, int dayproduction)
        {
            this.planetnumber = planetnumber;
            this.playerNumber = playerNumber;
            this.name = planetName;
            this.coords = coords;
            this.dayproduction = dayproduction;
            planetLooks = ChooseImage();
        }

        /// <summary>
        /// Get Method for everything this object contains.
        /// </summary>
        /// <returns></returns>
        public Tuple<int, String, Rectangle, int, Image> getAll()
        {
            Tuple<int, String, Rectangle, int, Image> all = new Tuple<int, String, Rectangle, int, Image>(playerNumber, name, coords, dayproduction, planetLooks);
            return all;
        }

        private String chooseName()
        {
            string[] alleNamen = File.ReadAllLines("planetnames.txt");
            
            return alleNamen[new Random().Next(0, alleNamen.Length-1)];
        }

        /// <summary>
        /// Method that chooses one of the Planet Images stored in the Planets Image folder
        /// </summary>
        /// <returns></returns>
        private Bitmap ChooseImage()
        {
            Image chosenPlanetImg;
            Bitmap chosenPlanetBitmap;
            string[] tmp = Directory.GetFiles("Galaxy Img/Planets/");

            Random random = new Random();
            int chosenPlanet = random.Next(0, tmp.Length);

            //Debug.WriteLine("planet path name : " + tmp[chosenPlanet]);

            chosenPlanetImg = Image.FromFile(tmp[chosenPlanet]);
            chosenPlanetBitmap = new Bitmap(chosenPlanetImg, 250, 250);

            //note : MakeTransparent is only appliable to Bitmap, NOT ON IMAGE!
            chosenPlanetBitmap.MakeTransparent(chosenPlanetBitmap.GetPixel(0, 0));

            return chosenPlanetBitmap;
        }

        /// <summary>
        /// Get method for the planets name.
        /// </summary>
        /// <returns></returns>
        public String getName()
        {
            return name;
        }

        /// <summary>
        /// Get method for the planets coordinates.
        /// </summary>
        /// <returns></returns>
        public Rectangle getCoords()
        {
            return coords;
        }

        /// <summary>
        /// Get method for the Image used to display the planet
        /// </summary>
        /// <returns></returns>
        public Bitmap getPlanetLooks()
        {
            return planetLooks;
        }

        public int getPlayernumber() {
            return playerNumber;
        }

        public int getPlanetnumber() {
            return planetnumber;
        }

        public void produceShips()
        {
            for (int i = 0; i < dayproduction; i++)
            {
                //ships.Add(new Ship(playerNumber, Ship.type.Distroyer));
                ships += dayproduction;
            }
        }

        public void sendShips(int shipstosend)
        {
            for (int i = 0; i < shipstosend; i++)
            {
                //ships.RemoveAt(i);
                ships -= shipstosend;
            }
        }

        public void receiveShips(int shipstoreceive)
        {
            for (int i = 0; i < shipstoreceive; i++)
            {
                //ships.Add(new Ship(playerNumber, Ship.type.Distroyer));
                ships += shipstoreceive;
            }
        }

        public int getAmountOfShips {
            get
            {
                //return ships.Count;
                return ships;
            }
        }
    }
}
